package com.sunny.example.string;

import java.util.Arrays;
import java.util.Locale;
import java.util.Scanner;

public class HJ36_字符串加密 {
    static String originAlpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

    public static void main(String[] args) {


        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            String pwd = scanner.nextLine();
            String plainText = scanner.nextLine();
            //密钥处理，去掉重复字母
            pwd = handlePwd(pwd);
            String newAlpha = genarateNewAlpha(pwd);
            String ecryptedText = getEncryptedText(plainText, newAlpha);
            System.out.println(ecryptedText);

        }

    }

    private static String getEncryptedText(String plainText, String newAlpha) {
        if (null == plainText || "".equals(plainText)) {
            return "";
        }
        StringBuffer stringBuffer = new StringBuffer();
        for (char c : plainText.toCharArray()) {
            //全部转为小写处理
            int index = originAlpha.toLowerCase(Locale.ROOT).indexOf(Character.toLowerCase(c));
            char ec = newAlpha.charAt(index);
            if (Character.isLowerCase(c)) {
                ec = Character.toLowerCase(ec);
            }
            stringBuffer.append(ec);

        }
        return stringBuffer.toString();
    }

    /**
     * 生成新的字母表
     *
     * @param str
     */
    private static String genarateNewAlpha(String str) {
        StringBuffer result = new StringBuffer(str);
        String tmp = originAlpha;
        for (int i = 0; i < str.length(); i++) {
            String s = String.valueOf(Character.toUpperCase(str.charAt(i)));
            tmp = tmp.replace(s, "");//转换成大写
        }
        result.append(tmp.substring(0, originAlpha.length() - str.length()));

        return result.toString();

    }

    private static String handlePwd(String pwd) {
        if (null == pwd || "".equals(pwd)) {
            return "";
        }
        StringBuffer sb = new StringBuffer();
        for (char c : pwd.toCharArray()) {
            if (!sb.toString().contains(String.valueOf(c))) {
                sb.append(c);
            }
        }
        return sb.toString();
    }
}
